package csc.com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import csc.com.util.User;

@WebServlet(name = "ListAllUsers", urlPatterns = { "/list-users"})
public class ListUsersServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		
		Connection connection = (Connection) getServletContext().getAttribute("DBConnection");
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			
			preparedStatement = connection.prepareStatement("select * from Users");
			resultSet = preparedStatement.executeQuery();
			List<User> lst = new LinkedList<User>();
			
			if (resultSet.next()) {
				// Fetch each row from the result set
				while (resultSet.next()) {
					String userName = resultSet.getString("username");
					String password = resultSet.getString("password");
					String address = resultSet.getString("address");
					String email = resultSet.getString("email");
					int countLogin = resultSet.getInt("countLogin");
					// Assuming you have a user object
					User user = new User(userName, password,address,email,countLogin);
					lst.add(user);
				}
			}
			
			RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/adminPage.html");
            PrintWriter out= resp.getWriter();
           
            String headerTable = "<table>" +
         			"<tr><th>User Name</th><th>Password</th><th>Address</th><th>Email</th><th>Update</th><th>Delete</th></tr>";
    		 out.println(headerTable);
            for (User user : lst) {
            	
            	String content = "<form action='action' method='post'><tr><td>" + user.getUserName()+"</td>" +
            				"<input type='hidden' name='username' value='"+ user.getUserName() +"'>" +
            			    "<td><input type='password' name='password' value='"+ user.getPassword() +"'></td>" +
            			    "<td><input type='text' name='address' value='"+ user.getAddress() +"'></td>" +
            			    "<td><input type='text' name='email' value='"+ user.getEmail() +"'></td>"
            			    		+"<td><input type='submit' name='action' value='update'></td>"
            			    		+ "<td><input type='submit' name='action' value='delete'></td></tr></form>";
            	 out.println(content);
            }
            out.println("</table>");
            requestDispatcher.include(req, resp);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new ServletException("DB Connection problem.");
		} finally {
			try {
				resultSet.close();
				preparedStatement.close();
			} catch (SQLException e) {
				
			}
		}
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	private static final long serialVersionUID = 626729781737369500L;

}
